<template>
  <div>
    <!--        搜索框-->
    <div class="pd-5">
      <el-input style="width: 200px" placeholder="请输入文件名" suffix-icon="el-icon-search" v-model="query.name"
                class="ml-5 mr-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入文件类型" suffix-icon="el-icon-s-order" v-model="query.type"
                class="ml-5 mr-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入最小值" suffix-icon="el-icon-remove-outline"
                v-model="query.size"
                class="ml-5 mr-5"></el-input>
      <el-input style="width: 200px" placeholder="请输入最大值" suffix-icon="el-icon-circle-plus-outline"
                v-model="query.mixSize"
                class="ml-5 mr-5"></el-input>
      <el-button type="primary" @click="loadBtn">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <div style="float: right">

        <el-upload
            class="upload-demo"
            action="http://localhost:8090/file/upload"
            :show-file-list="false"
            style="display: inline-block"
            :headers="heders"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess">
          <el-button type="primary">上传文件<i class="el-icon-circle-plus-outline"></i></el-button>
        </el-upload>

        <el-popconfirm
            class="mr-5 ml-5"
            confirm-button-text='确定'
            cancel-button-text='点错了'
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @confirm="delNumBtn()">
          <el-button type="danger" slot="reference">批量删除<i class="el-icon-remove-outline"></i></el-button>
        </el-popconfirm>
      </div>
    </div>

    <!--        列表-->
    <el-table :data="tableData" stripe border :header-cell-class-name="headerBg"
              @selection-change="handleSelectionChange">
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="name" label="文件名"></el-table-column>
      <el-table-column prop="type" label="文件类型"></el-table-column>
      <el-table-column prop="size" label="文件大小"></el-table-column>
      <el-table-column prop="enable" label="启用">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.enable" @change="enableChange(scope.row) "></el-switch>
        </template>
      </el-table-column>

      <el-table-column prop="123" label="下载">
        <template slot-scope="scope">
          <el-button type="success" @click="downloadBtn(scope.row.url)">下载<i class="el-icon-download"></i></el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作"
                       align="center"
                       fixed="right"
                       width="200px">
        <template slot-scope="scope">
          <el-popconfirm
              class="mr-5 ml-5"
              confirm-button-text='确定'
              cancel-button-text='点错了'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="delectBtn(scope.row.id)">
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <!--        分页-->
    <div class="pd-5">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>

  </div>
</template>

<script>

export default {
  data() {
    return {
      heders:{
        token:  JSON.parse(localStorage.getItem("user")).token
      },
      // 模态窗口样式
      user: {
        id: '',
        name: '',
        type: '',
        url: '',
        size: '',
        enable: 1,
      },
      // 分页数据
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 5,

      // 条件查询数据
      query: {
        name: '',
        type: '',
        size: '',
        mixSize: ''
      },

      // 批量删除数据
      multipleSelection: [],

      //编辑数据
      editBtnData: false,

      headerBg: 'headerBg',
      dialogFormVisible: false,
    }
  },
  methods: {
    //上传文件的限制
    beforeUpload(file){
      const isLt5M=file.size / 1024 / 1024 < 5
     if (!isLt5M){
        this.$message.warning("文件大小不能大于5M")
     }
     return isLt5M;
    },
    //启用
    enableChange(row) {
      this.request.post("/file/update", row)
    },
    //下载
    downloadBtn(url) {
      window.open(url)
    },
    // 上传文件
    uploadSuccess(res) {
      if (res.code == 200) {
        this.$message.success("上传成功")
        this.load()
      } else {
        this.$message.error(res.msg)
      }
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.load()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val
      this.load()
    },

    //查询
    load() {
      this.request.get("/file/page", {
        params: {
          num: this.pageNum,
          pagesize: this.pageSize,

          name: this.query.name,
          type: this.query.type,
          size: this.query.size,
          mixSize: this.query.mixSize,
        }
      }).then(res => {
            // console.log(res.data)
            if (res.code == 200) {
              this.tableData = res.data.records
              this.total = res.data.total
            }
          },
          Error => {
            console.log(Error)
          }
      )
    },
    //重置
    reset() {
      this.query.name = ''
      this.query.type = ''
      this.query.size = ''
      this.query.mixSize = ''
      this.load()
    },
    //搜索
    loadBtn() {
      //需将页面转为第一页，从头开始查询
      this.pageNum = 1
      this.load()
    },
    //删除
    delectBtn(id) {
      this.request.delete('/file/' + id).then(
          res => {
            if (res.code == 200) {
              this.$message.success("删除成功")
              this.load()
            }
          },
          Error => {
            this.$message.success("删除失败")
            console.log(Error)
          }
      )
    },
    //删除多个
    delNumBtn() {
      let ids = this.multipleSelection.map(v => v.id)
      if (ids.length == 0) {
        this.$message.error("请选择要删除的用户")
        return;
      }
      this.request.delete('/file/batch', {data: ids}).then(res => {
            if (res) {
              if (res.code == 200) {
                this.$message.success("删除成功")
                this.load()
              }
            } else {
              this.$message.error("删除失败")
              console.log(Error)
            }
          }
      )
    },
    //多选框事件
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
  created() {
    this.load()
  },
}
</script>


<style scoped>

</style>